#include "doc_edit_widget.h"
#include "ui_doc_edit_widget.h"
#include <QDebug>
#include <QTextTable>
#include "easylogging++.h"

QHash<QString, int> gFontSizeMap = {
    {QStringLiteral("初号"),72},
    {QStringLiteral("小初"), 36},
    {QStringLiteral("一号"), 28},
    {QStringLiteral("小一"), 24},
    {QStringLiteral("二号"), 20},
    {QStringLiteral("小二"), 18},
    {QStringLiteral("三号"), 16},
    {QStringLiteral("小三"), 14},
    {QStringLiteral("四号"), 12},
    {QStringLiteral("小四"), 10},
    {QStringLiteral("五号"), 9},
    {QStringLiteral("小五"), 8},
};


DocEditWidget::DocEditWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DocEditWidget)
{
    ui->setupUi(this);
    initUi();
    initEvents();
}

DocEditWidget::~DocEditWidget()
{
    delete ui;
}

QString DocEditWidget::getDoc()
{
    return ui->textEdit->toHtml();
}

void DocEditWidget::setDoc(const QString &content)
{
    ui->textEdit->setHtml(content);
}

void DocEditWidget::initUi()
{
    initFontStyle();
}

void DocEditWidget::initEvents()
{
    connect(ui->docEditorMenuBarWidget, &DocEditorMenuBarWidget::saveTriggered, [=](){
        emit(saveDocTriggered());
    });
    connect(ui->docEditorMenuBarWidget, &DocEditorMenuBarWidget::insertTable,
            this, &DocEditWidget::onInsertTable);

    connect(ui->docEditorMenuBarWidget, &DocEditorMenuBarWidget::selectFontFamily, this, [=](QString fontFamily){
        QTextCursor cursor = ui->textEdit->textCursor();
        // 创建一个字体对象，并设置字体样式
        QFont font = cursor.charFormat().font();
        font.setFamily(fontFamily);
        QTextCharFormat charFormat = cursor.charFormat();
        charFormat.setFont(font);
        // 将字体应用到选中的文本
        cursor.setCharFormat(charFormat);

        // 将修改后的 QTextCursor 应用到 QTextEdit
        ui->textEdit->setTextCursor(cursor);
    });
    connect(ui->docEditorMenuBarWidget, &DocEditorMenuBarWidget::selectFontSize, this, [=](int pixelSize) {
        QTextCursor cursor = ui->textEdit->textCursor();
        QFont font = cursor.charFormat().font();// 创建一个字体对象，并设置字体样式
        font.setPixelSize(pixelSize);
        QTextCharFormat charFormat = cursor.charFormat();
        charFormat.setFont(font);
        cursor.setCharFormat(charFormat);       // 将字体应用到选中的文本
        ui->textEdit->setTextCursor(cursor);    // 将修改后的 QTextCursor 应用到 QTextEdit
    });

    connect(ui->docEditorMenuBarWidget, &DocEditorMenuBarWidget::fontBold, this, [=](bool checked) {
        QTextCursor cursor = ui->textEdit->textCursor();
        QFont font = cursor.charFormat().font();// 创建一个字体对象，并设置字体样式
        font.setBold(checked);
        QTextCharFormat charFormat = cursor.charFormat();
        charFormat.setFont(font);
        cursor.setCharFormat(charFormat);       // 将字体应用到选中的文本
        ui->textEdit->setTextCursor(cursor);    // 将修改后的 QTextCursor 应用到 QTextEdit
    });

    connect(ui->docEditorMenuBarWidget, &DocEditorMenuBarWidget::fontIncline, this, [=](bool checked) {
        QTextCursor cursor = ui->textEdit->textCursor();
        QFont font = cursor.charFormat().font();// 创建一个字体对象，并设置字体样式
        font.setItalic(checked);
        QTextCharFormat charFormat = cursor.charFormat();
        charFormat.setFont(font);
        cursor.setCharFormat(charFormat);       // 将字体应用到选中的文本
        ui->textEdit->setTextCursor(cursor);    // 将修改后的 QTextCursor 应用到 QTextEdit
    });

    connect(ui->textEdit, &QTextEdit::currentCharFormatChanged, this, [=](const QTextCharFormat &textCharFormat){
        QFont   font        = textCharFormat.font();
        QString fontFamily  = font.family();
        float   fontSize    = font.pixelSize();
        bool    fontBold    = font.bold();
        bool    fontIncline = font.italic();

        ui->docEditorMenuBarWidget->setFontFamily(fontFamily);
        ui->docEditorMenuBarWidget->setFontSize(fontSize);
        ui->docEditorMenuBarWidget->setFontBold(fontBold);
        ui->docEditorMenuBarWidget->setFontIncline(fontIncline);
    });
}

void DocEditWidget::initFontStyle()
{
    QTextCursor cursor = ui->textEdit->textCursor();
    // 创建一个字体对象，并设置字体样式
    QFont font = cursor.charFormat().font();
    QString fontFamily = QStringLiteral("宋体");
    font.setFamily(fontFamily);
    font.setPixelSize(16);
    QTextCharFormat charFormat = cursor.charFormat();
    charFormat.setFont(font);
    // 将字体应用到选中的文本
    cursor.setCharFormat(charFormat);

    // 将修改后的 QTextCursor 应用到 QTextEdit
    ui->textEdit->setTextCursor(cursor);

    QTextDocument *doc = ui->textEdit->document();
    QTextBlockFormat blockFormat = doc->begin().blockFormat();
    blockFormat.setLineHeight(150, QTextBlockFormat::ProportionalHeight);
    cursor.setBlockFormat(blockFormat);
    ui->docEditorMenuBarWidget->setFontFamily(fontFamily);
    ui->docEditorMenuBarWidget->setFontSize(16);

    ui->textEdit->append("<img src=\"image.png\" />");
}

void DocEditWidget::onInsertTable(int rows, int cols)
{
    QTextCursor cursor(ui->textEdit->textCursor());
    QTextTable *table = cursor.insertTable(rows, cols);
    //获取表格的格式
    QTextTableFormat tableFormat = table->format();
    //表格格式设置宽度
    tableFormat.setWidth(QTextLength(QTextLength::VariableLength, ui->textEdit->width()-100));
    //表格格式设置启用BorderCollapse
    tableFormat.setBorderCollapse(true);

    //设置表格的columnWidthConstraints约束
    QVector<QTextLength> colLength = tableFormat.columnWidthConstraints();
    for (int i = 0; i < cols; ++i) {
        colLength.append(QTextLength(QTextLength::FixedLength,tableFormat.width().rawValue()/cols));
    }
    tableFormat.setColumnWidthConstraints(colLength);
    tableFormat.setBorder(1);
    tableFormat.setBorderBrush(Qt::black);


    //定义单元格格式
    QTextTableCellFormat cellFormat;
    cellFormat.setBottomBorderStyle(QTextFrameFormat::BorderStyle_Solid);
    cellFormat.setBottomBorder(1);
    cellFormat.setBottomBorderBrush(Qt::red);
    cellFormat.setBackground(QColor("moccasin"));

    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            QTextTableCell cell = table->cellAt(i,j);
            if(i == 0)
            {
                cell.setFormat(cellFormat);
            }

            QTextCursor cellCursor = cell.firstCursorPosition();
            QTextBlockFormat cellBlockFormat = cellCursor.blockFormat();
            cellBlockFormat.setProperty(QTextFormat::BlockAlignment,Qt::AlignLeft);
            cellCursor.setBlockFormat(cellBlockFormat);
        }
    }

    table->setFormat(tableFormat);
}
